/**
  * ProjectName doctor
 * FileName DepartmentServiceImpl.java
 * PackageName com.doctor.service.impl
 * Date 2017年6月12日 下午1:03:17
 * Copyright (c) 2015, lyg945@163.com All Rights Reserved.
 */
package com.doctor.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.doctor.entity.Department;
import com.doctor.entity.SSmsRecord;
import com.doctor.exception.ServiceException;
import com.doctor.repository.DepartmentRepository;
import com.doctor.service.DeparmentService;
import com.doctor.utils.SmsUtils;

/**
 * @ClassName DepartmentServiceImpl 
 * @Function TODO
 * @Date  2017年6月12日 下午1:03:17
 * @author 刘永刚
 * @version V1.00 
 * @since JDK 1.8  
 * @see
 */
@Service
public class DepartmentServiceImpl implements DeparmentService {

	@Resource
	private DepartmentRepository departmentRepository;
	
	@Override
	@Cacheable(value="department")
	public List<Department> findAll(){
		return departmentRepository.findAll();
	}

	@Override
	@Cacheable(value="sms",key="#mobile")
	public int smsCode(String mobile) {
		int code = (int) Math.round(Math.random()*1000000);
		SmsUtils util = new SmsUtils(mobile, "{\"code\":"+code+"}");
		SSmsRecord ssm = util.send(mobile, "18602116219");
		return code;
	}

	@Override
	public Page<Department> findAll(PageRequest pageRequest) {
		return departmentRepository.findAll(pageRequest);
	}

	@Override
	public Department findById(Long id) {
		return departmentRepository.findOne(id);
	}

	@Override
	public void saveOrUpdate(Department department) {
		if(department == null){
			throw new ServiceException("科室不能为空");
	    }
		departmentRepository.saveAndFlush(department);
	}

	@Override
	public void delete(Long id) {
		departmentRepository.delete(id);
	}
	
	
}
